package com.xm.service.module.USER.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xm.service.framework.util.Result;
import com.xm.service.module.USER.mapper.UserMapper;
import com.xm.service.module.USER.po.User;
import com.xm.service.module.USER.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(int uid, String password) {
        User user = userMapper.queryByIdAndName(uid,password); // 根据用户名查询用户信息
        if (user != null && user.getPassword().equals(password)) { // 如果用户存在且密码正确，则返回对应的User对象。否则返回null。
            System.out.println(user);
            return user;
        } else { // 如果用户不存在或密码错误，则返回null。
            return null;
        }
    }

    @Override
    public Result creatorUser(String uname, String password, String email,
                              int age, String gender) {
        if (userMapper.queryName(uname) != null) {
            return Result.error("0","用户名已被占用！");
        } else if (userMapper.queryEmail(email) != null) {
            return Result.error("0", "注册失败，邮箱已被占用");
        }else {
            String uid = String.valueOf(userMapper.creatorUser(uname, password, email, age, gender));
            User user = userMapper.queryByName(uname);
            return Result.success(user, uid);
        }
    }
}
